package com.woniuxy.educationplatform.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.educationplatform.bean.*;
import com.woniuxy.educationplatform.mapper.*;
import com.woniuxy.educationplatform.service.IStudentService;
import com.woniuxy.educationplatform.util.Md5Util;
import com.woniuxy.educationplatform.util.PageDto;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional(readOnly = true)
public class StudentServiceImpl implements IStudentService {
    @Resource
    private StudentMapper studentMapper;
    //增加了SchoolMapper的自动填充
    @Resource
    private SchoolMapper schoolMapperImpl;

    @Resource
    private StuVideosMapper stuVideosMapper;

    @Resource
    private StuTestMapper stuTestMapper;

    @Override
    public List<StudentBean> findAllByParams() {
        return null;
    }

    @Override
    public SchoolBean selectByID(long ID) {
        return schoolMapperImpl.selectByID(ID);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateStudentBean(StudentBean studentBean) {
        return studentMapper.updateStudentBean(studentBean);
    }

    @Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
    @Override
    public int deleteStudentBean(Long id) {
        return studentMapper.deleteStudentBean(id);
    }

    @Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
    @Override
    public int deleteStuVideoBean(Long id) {
        return stuVideosMapper.deleteStuVideoBean(id);
    }

    @Override
    public List<StudentBean> findAllAndVideoes() {
        return null;
    }

    @Override
    public List<SchoolBean> selectAllSchool() {
        return schoolMapperImpl.selectAllSchool();
    }


    @Override
    public List<StudentBean> findAllStudentRecording(StudentBean studentBean) {
        return studentMapper.findAllStudentRecording(studentBean);
    }

    @Override
    public List<StudentBean> findAllStudentDetails(StudentBean studentBean) {
        return studentMapper.findAllStudentDetails(studentBean);
    }

    @Override
    public StudentBean findStudentById(int id) {
        return studentMapper.findStudentById(id);
    }

    @Override
    public List<StudentBean> findStudentBySchoolID(Integer schoolID) {
        return studentMapper.findStudentBySchoolID(schoolID);
    }

    @Override
    public List<StudentBean> findStudent(StudentBean studentBean) {
        return studentMapper.findStudent(studentBean);
    }

    @Override
    public PageDto findAllByParams(StudentBean studentBean, int pageNum, int pageSize) {
        //页码非法则直接返回
        if (pageNum <= 0) {
            return null;
        }
        PageHelper.startPage(pageNum, pageSize);
        List<StudentBean> data = studentMapper.findAllByParams(studentBean);
        PageInfo<StudentBean> pageInfo = new PageInfo<>(data);

        PageDto pageDto = new PageDto();
        //返回一共多少条数据
        pageDto.setTotal(pageInfo.getTotal());
        //返会数据
        pageDto.setData(data);
        return pageDto;
    }

    @Override
    public PageDto findAllClassByParams(StudentBean studentBean, int pageNum, int pageSize) {
        //页码非法则直接返回
        if (pageNum <= 0) {
            return null;
        }
        PageHelper.startPage(pageNum, pageSize);
        List<StudentBean> data = studentMapper.findAllClassByParams(studentBean);
        PageInfo<StudentBean> pageInfo = new PageInfo<>(data);

        PageDto pageDto = new PageDto();
        //返回一共多少条数据
        pageDto.setTotal(pageInfo.getTotal());
        //返会数据
        pageDto.setData(data);
        return pageDto;
    }

    @Override
    public PageDto findAllRecordsByParams(StuVideosBean stuVideosBean, int pageNum, int pageSize) {
        //页码非法则直接返回
        if (pageNum <= 0) {
            return null;
        }
        PageHelper.startPage(pageNum, pageSize);
        List<StuVideosBean> data = stuVideosMapper.findAllByParams(stuVideosBean);
        PageInfo<StuVideosBean> pageInfo = new PageInfo<>(data);

        PageDto pageDto = new PageDto();
        //返回一共多少条数据
        pageDto.setTotal(pageInfo.getTotal());
        //返会数据
        pageDto.setData(data);
        return pageDto;
    }

    @Override
    public List<StuTestBean> findAllTestByParams(int stuId) {
        List<StuTestBean> data = stuTestMapper.findAllByParams(stuId);
        return data;
    }


}
